package zhoug.demo.common;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.SparseArray;
import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import zhoug.common.adapters.recycler2.BaseRecyclerViewAdapter;
import zhoug.common.adapters.recycler2.BaseViewHolder;
import zhoug.common.compat.BundleCompat;
import zhoug.common.compat.IBundle;
import zhoug.common.json.JsonUtils;
import zhoug.common.router.Router;
import zhoug.demo.common.fileselector.TestFileSelectorActivity;
import zhoug.logging.Logger;

public class MainActivity extends MyBaseActivity {
    private static final String TAG = ">>>MainActivity";

    private static final List<ActivityUrl> URLS= Arrays.asList(
            new ActivityUrl("拨打电话",CallPhoneActivity.class),
            new ActivityUrl("文件选择器", TestFileSelectorActivity.class)
    );

    private RecyclerView mRecyclerView;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_main;
    }

    @Override
    protected void findViews() {

        mRecyclerView = findViewById(R.id.recyclerView);
    }

    @Override
    protected void onCreate1(Bundle savedInstanceState) {
        testBundle();
        UrlAdapter adapter=new UrlAdapter(this);
        adapter.setData(URLS);
        mRecyclerView.setAdapter(adapter);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

    }

    private static final class UrlAdapter extends BaseRecyclerViewAdapter<ActivityUrl> implements View.OnClickListener {

        public UrlAdapter(Context context) {
            super(context);
        }

        @Override
        protected int getLayoutId(int viewType) {
            return R.layout.main_test_menu_item;
        }

        @Override
        protected void onBindData(BaseViewHolder holder, ActivityUrl data, int position, int viewType) {
            TextView mTvName = holder.itemView.findViewById(R.id.tv_name);
            if(data!=null){
                mTvName.setText(data.getName());
                holder.itemView.setTag(position);
                holder.itemView.setClickable(true);
                holder.itemView.setOnClickListener(this);
            }
        }


        @Override
        public void onClick(View v) {
            ActivityUrl itemData = getItemData((Integer) v.getTag());
            if(null != itemData){
                Router.navigation(getContext(),itemData.getCls());
            }
        }
    }

    private static final class ActivityUrl{
        private final String name;
        private final Class<? extends Activity> cls;

        public ActivityUrl(String name, Class<? extends Activity> cls) {
            this.name = name;
            this.cls = cls;
        }

        public String getName() {
            return name;
        }

        public Class<? extends Activity> getCls() {
            return cls;
        }

    }


    private void testBundle(){

        Firm firm1=new Firm();
        firm1.setFirmName("琴马小学");
        firm1.setBuildYear(4);
        Firm firm2=new Firm();
        firm2.setFirmName("南宁中学");
        firm2.setBuildYear(8);
        User user1=new User();
        user1.setName("张三");
        user1.setAge(22);
        user1.setAdmin(true);
        User user2=new User();
        user2.setName("李四");
        user2.setAge(36);
        user2.setAdmin(false);

        ArrayList<Firm> firms=new ArrayList<>();
        firms.add(firm1);
        firms.add(firm2);
        SparseArray<Firm> firmSparseArray=new SparseArray<>();
        firmSparseArray.put(1,firm1);
        firmSparseArray.put(2,firm2);

        ArrayList<User> users=new ArrayList<>();
        users.add(user1);
        users.add(user2);

        IBundle iBundle = BundleCompat.create(new Bundle());
        iBundle.putParcelable("putParcelable",firm1)
                .putParcelableArray("putParcelableArray",new Firm[]{firm1,firm1})
                .putParcelableArrayList("putParcelableArrayList",firms)
                .putSparseParcelableArray("putSparseParcelableArray",firmSparseArray)
                .putSerializable("putSerializable",user1)
                .putSerializableArrayList("users",users);

        Bundle data = iBundle.getData();
        Logger.d(TAG, "testBundle:"+data);
        IBundle iBundle1 = BundleCompat.create(data);
        Firm putParcelable = iBundle1.getParcelable("putParcelable", Firm.class);
        log(putParcelable);
        Firm[] putParcelableArrays = iBundle1.getParcelableArray("putParcelableArray", Firm.class);
        log(putParcelableArrays);
        ArrayList<Firm> putParcelableArrayList = iBundle1.getParcelableArrayList("putParcelableArrayList", Firm.class);
        log(putParcelableArrayList);
        SparseArray<Firm> putSparseParcelableArray = iBundle1.getSparseParcelableArray("putSparseParcelableArray", Firm.class);
        log(putSparseParcelableArray);
        User putSerializable = iBundle1.getSerializable("putSerializable", User.class);
        log(putSerializable);
        ArrayList<User> users1 = iBundle1.getSerializableArrayList("users", User.class);
        log(users1);
        ArrayList<User> users2 = iBundle1.getSerializableArrayList("putParcelableArray", User.class);
        log(users2);

    }

    private void log(Object obj){
        if(obj==null){
            Logger.e(TAG, "null");
        }else{
            Logger.d(TAG, obj.getClass().getName());
            Logger.d(TAG, JsonUtils.toJson(obj));
        }
        Logger.d(TAG, "-----------------------------------");
    }

}